package cn.initcap.concurrency.concurrency.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;

/**
 * 定时执行线程
 *
 * @author initcap
 * @date Created in 2018/6/18 PM3:48.
 */
@Slf4j
public class ThreadPoolScheduledThreadPool {

    public static void main(String[] args) {
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(5,
                new ThreadFactoryBuilder().setNameFormat("scheduled").build());

//        executorService.schedule(() -> log.warn("schedule run"),3, TimeUnit.SECONDS);

        // 注意这个方法不适合调用executorService.shutdown();
        executorService.scheduleAtFixedRate(() -> log.warn("schedule run"), 1, 3, TimeUnit.SECONDS);
    }

}
